<?php
include_once("../configure/configure.php");		//--> global var
include_once("../admin/configure/admin.config.inc.php");		//--> global var

include_once("apis/CjApi.php");
include_once("impl/ApiLogImpl.php");

include_once("utils/StringUtils.php");

include_once("impl/MerchantImpl.php");
include_once("impl/AdvertiserImpl.php");


set_time_limit(0);


$merImpl 	= new MerchantImpl(DB_TAG_COUPON);
$apiLogImpl	= new ApiLogImpl(DB_TAG_COUPON);
//$feedImpl	= new FeedImpl(DB_TAG_COUPON, DB_TAG_FEED);
$adImpl 	= new AdvertiserImpl(DB_TAG_COUPON);

$startTime = $apiLogImpl->getLastRequestFeedTime(GlobalConstant::SOURCE_ID_CJ);

$vo = new LogRequestFeedDomain();
$vo->setStatus('1');
$vo->setSourceId(GlobalConstant::SOURCE_ID_CJ);
$vo->setStartTime($startTime);
$apiLogImpl->addRequestFeed($vo);


$apiCj	= new CjApi();

$advers = $apiCj->getAdvertisers(100, 100, 'joined');
//var_dump($links);


echo "advertiser count: " . count($advers);
echo "\n";

$existedMerchantCount 	= 0;
$newMerchantCount		= 0;

foreach($advers as $adver){
//	echo "------------------------------\n";
//	echo "advertiser-id: ". $adver->advertiserId . "\n";
//   echo "advertiser-name: ". $adver->advertiserName . "\n";
//    echo "accountStatus: ". $adver->accountStatus . "\n";
//    echo "programUrl: ". $adver->programUrl . "\n";
//    echo "mobileTrackingCertified: ". $adver->mobileTrackingCertified . "\n";
//    echo "networkRank: ". $adver->networkRank . "\n";
//    echo "language: ". $adver->language . "\n";
//    echo "primaryCategory: ". $adver->primaryCategory . "\n";
//    echo "performanceIncentives: ". $adver->performanceIncentives . "\n";
//    echo "actions: ". $adver->actions . "\n";
//    echo "linkTypes: ". $adver->linkTypes . "\n";
//    echo "relationshipStatus: ". $adver->relationshipStatus . "\n";
//    echo "threeMonthEpc: ". $adver->threeMonthEpc . "\n";
//    echo "sevenDayEpc: ". $adver->sevenDayEpc . "\n";
//    echo "getChildCategory: ". $adver->getChildCategory() . "\n";

	$oMer = $merImpl->getByAdvertiserId(GlobalConstant::SOURCE_ID_CJ, $adver->advertiserId);		
	// find merchant by alias
	
	if(!is_object($oMer)){		
		$oMer = $merImpl->getByAlias($adver->advertiserName);
		
		if(!is_object($oMer)){			
			$oMer = $merImpl->getByName($adver->advertiserName);
		}
		
		if(!is_object($oMer)){			
			$oMer = $merImpl->getByDisplayUrl($adver->programUrl);
		}
		
	}
		
	if(is_object($oMer)){
		$existedMerchantCount++;
		
		echo "update merchant id: " . $oMer->getId();
		echo "\n";	
		
		echo "update advertiser Id: " . $adver->advertiserId;
		echo "\n";
		$adImpl->updateMerchantByCjAdvertiser($oMer, $adver);
	}else{
		$newMerchantCount++;
		
		echo "insert advertiser Id: " . $adver->advertiserId;
		echo "\n";
		
		$adImpl->addMerchantByCjAdvertiser($adver);
	}
	


}


echo "\n newMerchantCount 		= $newMerchantCount";
echo "\n existedMerchantCount 	= $existedMerchantCount";
echo "\n";

?>